﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PankowGame13
{
    class FloatingText
    {

        public Vector2 Position { get; set; }

        int _ttl = 0;
        int _life = 0;
        public bool Alive { get { return _ttl > 0; } }
        int _speed = 20;     //pixels per second

        string _text;
        public string Text { get { return _text; } }
        public Color textColor { get; set; }
        public float Fade { get { return (float)_ttl / (float)_life; } }
        //public byte Alpha { get { return (byte)((float)((float)_ttl / (float)_life) * 255); } }

        /// <summary>
        /// FloatingText (text) will rise above the position for ttl milliseconds.
        /// How to use: create new FloatingText(). Pass position, text and ttl.
        /// Add it to a list of FloatingTexts.
        /// Use Update() to let the FloatingText object update it's position and lifetime.
        /// If FloatingText object is not Alive, you can remove it from your list.
        /// 
        /// List&lt;FloatingText&gt; floatingTexts = new List&lt;FloatingText&gt;();
        /// floatingTexts.Add(new FloatingText(new Vector2(100,100),"Testing!",2000);
        /// 
        /// for (int i=0; i &lt; floatingTexts.Count; i++) {
        ///     floatingTexts[i].Update();
        ///     if (!floatingTexts[i].Alive) {
        ///         floatingTexts.Remove(floatingTexts[i]);
        ///         i--;
        ///     }
        /// }
        /// 
        /// foreach (FloatingText ft in floatingTexts) {
        ///     spriteBatch.DrawString(font, ft.Text, ft.Position, new Color(ft.Alpha, 0, 0, ft.Alpha));
        /// }
        ///     
        /// 
        /// </summary>
        /// <param name="position">Where to start the floating text</param>
        /// <param name="text">What to float</param>
        /// <param name="ttl">How long til it fades away in milliseconds</param>
        public FloatingText(Vector2 position, string text, Color c, int ttl)
        {
            this.textColor = c;
            this.Position = position;
            this._ttl = ttl;
            this._life = ttl;
            this._text = text;
        }

        /// <summary>
        /// Updates the FloatingText.
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            _ttl -= gameTime.ElapsedGameTime.Milliseconds;
            Position = new Vector2(Position.X, Position.Y - (float)_speed * (float)gameTime.ElapsedGameTime.Milliseconds/1000);
        }

        /// <summary>
        /// Draws the FloatingText.
        /// </summary>
        /// <param name="sb"></param>
        public void Draw(SpriteBatch sb, SpriteFont font)
        {
            Color c = new Color(
                (int)(textColor.R * this.Fade),
                (int)(textColor.G * this.Fade),
                (int)(textColor.B * this.Fade),
                (int)(255     * this.Fade)
                );

            sb.DrawString(font, this.Text, this.Position, c);
        }
    }
}
